package cn.exrick.xboot.modules.your.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.your.entity.Malfunction;
import cn.exrick.xboot.modules.your.service.IMalfunctionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chenyunxuan
 */
@Slf4j
@RestController
@Api(description = "陪护床管理接口")
@RequestMapping("/xboot/malfunction")
@Transactional
public class MalfunctionController {

    @Autowired
    private IMalfunctionService iMalfunctionService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<Malfunction> get(@PathVariable String id){

        Malfunction malfunction = iMalfunctionService.getById(id);
        return new ResultUtil<Malfunction>().setData(malfunction);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<Malfunction>> getAll(){

        List<Malfunction> list = iMalfunctionService.list();
        return new ResultUtil<List<Malfunction>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<Malfunction>> getByPage(@ModelAttribute PageVo page,@ModelAttribute Malfunction malfunction){
        QueryWrapper<Malfunction> qw=new QueryWrapper<>();
        IPage<Malfunction> data = iMalfunctionService.malfunctionPage(PageUtil.initMpPage(page),qw);
        return new ResultUtil<IPage<Malfunction>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<Malfunction> saveOrUpdate(@ModelAttribute Malfunction malfunction){

        if(iMalfunctionService.saveOrUpdate(malfunction)){
            return new ResultUtil<Malfunction>().setData(malfunction);
        }
        return new ResultUtil<Malfunction>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids){

        for(String id : ids){
            iMalfunctionService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }
}
